//
//  MPBidTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/6.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPFilterResultCell.h"
#import "MPFilterResultItem.h"
#import "UIImage+Common.h"

@interface MPFilterResultCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UILabel* contentLabel;
@property (nonatomic, strong)UIImageView* bgImageView;
@property (nonatomic, strong)UIImageView* accessoryImageView;
@property (nonatomic, strong)UIImageView* topSeperatorLineImageView;
@property (nonatomic, strong)UIImageView* bottomSeperatorLineImageView;
@end

@implementation MPFilterResultCell

+ (CGFloat)rowHeightForObject:(id)object
{
    return 80.0f;
}

+ (CGFloat)estimateRowsHeight
{
    return 80.0f;
}

+ (BOOL)shouldUseAutoLayout
{
    return NO;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        // TODO 去掉默认的高度为44的约束
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [self.contentView addSubview:self.topSeperatorLineImageView];
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.accessoryImageView];
//        [self.contentView addSubview:self.bottomSeperatorLineImageView];
        
        [self setupConstraints];
        [self.contentView setNeedsLayout];
        [self.contentView layoutIfNeeded];
        [self.bgImageView setImage:[UIImage imageFromColor:[UIColor whiteColor] imageSize:self.bgImageView.frame.size]];
        [self.topSeperatorLineImageView setImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor] imageSize:self.topSeperatorLineImageView.frame.size]];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.topSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).with.offset(10.0f);
        make.height.mas_equalTo(@(1.0f));
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topSeperatorLineImageView.mas_bottom);
        make.leading.bottom.trailing.mas_equalTo(self.contentView);
    }];
    
//    [self.bottomSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.bgImageView.mas_bottom);
//        make.leading.trailing.bottom.mas_equalTo(self.contentView);
//    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topSeperatorLineImageView.mas_bottom).with.offset(15.0f);
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(15.0f);
        make.leading.trailing.mas_equalTo(self.titleLabel);
//        make.bottom.mas_equalTo(self.contentView).with.offset(-10.0f);
    }];
    
    [self.accessoryImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).with.offset(5.0f);
        make.trailing.mas_equalTo(self.contentView).with.offset(-10.0f);
        make.width.mas_equalTo(@18.0f);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).with.offset(10.0f);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPFilterResultItem class]]) {
            MPFilterResultItem* item = (MPFilterResultItem *)object;
            if (![NSString stringIsEmpty:item.title]) {
                self.titleLabel.text = item.title;
            }
            
            if (![NSString stringIsEmpty:item.content]) {
                self.contentLabel.text = item.content;
            }
        }
    }
}


#pragma mark - property
- (UIImageView *)topSeperatorLineImageView
{
    if (!_topSeperatorLineImageView) {
        _topSeperatorLineImageView = [[UIImageView alloc] init];
    }
    return _topSeperatorLineImageView;
}

- (UIImageView *)bottomSeperatorLineImageView
{
    if (!_bottomSeperatorLineImageView) {
        _bottomSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor] imageSize:CGSizeMake(SCREEN_WIDTH, 1.0f)]];
    }
    return _bottomSeperatorLineImageView;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
    }
    return _bgImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
        _titleLabel.text = @"已选择平台";
    }
    return _titleLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _contentLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _contentLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _contentLabel;
}

- (UIImageView *)accessoryImageView
{
    if (!_accessoryImageView) {
        _accessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageForKey:@"accessory_icon"]];
    }
    return _accessoryImageView;
}


@end
